
<script type="text/javascript">
	$(document).ready(function() {
		$("#invites").tablesorter({
			widthFixed: true,
			widgets: ['zebra']
		}).tablesorterPager({
			container: $(".pager")
		});
	});

	jQuery(document).ajaxStart(function() {
		var progress = noty({
			id: "progress",
			text: $(document.createElement('div')).append(
					$(document.createElement('img')).attr({
						width : "12",
						height : "12",
						alt: "",
						src: "<?php echo base_url().'theme/default/images/loading.gif'; ?>"
					}),
					$(document.createElement('span')).text("  memproses request...")
				),
			type: "alert",
			layout: "top",
			theme: "default",
			timeout: 5000,
			animation: {
				open: {height: 'toggle'},
			    close: {height: 'toggle'},
			    easing: 'swing',
				speed: 200
			}
		});
	});
	
	jQuery(document).ajaxStop(function() {
		$.noty.close("progress");
	});

	function saveinvite(obj) {
		var name = $(obj).val();
		var checked = $(obj).is(":checked");
		var token = $("input[name=smb_36742cef0aed]").val();
		var id = "<?php echo $scholarship->id; ?>";
		$.ajax({
			type: "POST",
			url: <?php echo '"'.site_url($role.'/scholar/offer/saveinvite').'"'; ?>,
			data: { id : id, name : name, 'smb_36742cef0aed' : token, checked : checked },
			dataType: "json",
			success: function(data) {
				var n = noty({
					text: data.message,
					type: data.type,
					dismissQueue: true,
					layout: 'center',
					theme: 'default',
					timeout: 1000,
					animation: {
						open: {height: 'toggle'},
					    close: {height: 'toggle'},
					    easing: 'swing',
						speed: 200
					}
				});
				if (data.status == "failed") {
					if (checked == false) {
						$(obj).attr("checked", "checked");
					} else {
						$(obj).removeAttr("checked");
					}
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				var n = noty({
					text: errorThrown,
					type: "error",
					dismissQueue: true,
					layout: 'center',
					theme: 'default',
					timeout: 1000,
					animation: {
						open: {height: 'toggle'},
					    close: {height: 'toggle'},
					    easing: 'swing',
						speed: 200
					}
				});
				if (checked == false) {
					$(obj).attr("checked", "checked");
				} else {
					$(obj).removeAttr("checked");
				}
			}
		});
	}
</script>

<div id="main-content" class="prepend-1 span-19 last">
	<h3>Undang Mahasiswa ke Beasiswa - <?php echo anchor($role.'/scholar/offer/detail/'.$scholarship->id, $scholarship->name); ?></h3>
	<h4>Form Pencarian Mahasiswa</h4>
<?php echo form_open($role.'/scholar/offer/invite/'.$scholarship->id, array('method' => 'get', 'class' => 'form')); ?>
	<table>
		<tr>
			<td width="200"><label>Nama Mahasiswa</label></td>
			<td><input type="text" name="name" maxlength="64" style="width: 400px!important" value="<?php echo ext_set_value('name'); ?>" /></td>
		</tr>
		<tr>
			<td><label>Angkatan</label></td>
			<td>
				<select name="year">
					<option value="" <?php echo ext_set_select('year', '', TRUE); ?>>--Silakan pilih tahun--</option>
<?php 
	foreach ($years as $year) {
		echo '<option value="'.$year->year.'" '.ext_set_select('year', $year->year).'>'.$year->year.'</option>';
	}
?>
				</select>
			</td>
		</tr>
		<tr>
			<td><label>Program Studi</label></td>
			<td>
				<select name="major">
					<option value="" <?php echo ext_set_select('major', '', TRUE); ?>>--Silakan pilih program studi--</option>
<?php 
	foreach ($majors as $major) {
		echo '<option value="'.$major->id.'" '.ext_set_select('major', $major->id).'>'.$major->major.'</option>';
	}
?>
				</select>
			</td>
		</tr>
		<tr>
			<td><label>IPK</label></td>
			<td>
				<input type="text" name="gpa" maxlength="4" value="<?php echo ext_set_value('gpa'); ?>" />
				<?php if (isset($gpavalid)) echo $gpavalid; ?>
			</td>
		</tr>
	</table>
	<input type="submit" name="search" value="Cari" class="submit" />
<?php echo form_close(); ?>
	<div class="clear"><?php echo nbs(); ?></div>
	<h4><?php echo $title; ?></h4>
<?php echo form_open(); ?>
	<table id="invites">
	<thead>
		<tr>
			<th>Nama Mahasiswa</th>
			<th width="100">Angkatan</th>
			<th width="250">Program Studi</th>
			<th width="50">IPK</th>
			<th width="50">Undang</th>
		</tr>
	</thead>
	<tfoot>
		<?php echo $pager; ?>
	</tfoot>
	<tbody>
<?php 
	if ($students == NULL) :
?>
		<tr>
			<td colspan="5" align="center"><span class="info"><?php echo $empty; ?></span></td>
		</tr>
<?php 
	else :
		foreach ($students as $student) :
?>
		<tr>
			<td><?php echo anchor($role.'/student/profile/index?id='.$student->id, $student->name, array('target' => '_blank')); ?></td>
			<td align="center"><?php echo $student->year; ?></td>
			<td><?php echo $student->major; ?></td>
			<td align="center"><?php echo $student->gpa; ?></td>
<?php if ($student->status == '') : ?>
			<td align="center"><input type="checkbox" name="invites[]" value="<?php echo $student->username.'@'.$student->name; ?>" onchange="saveinvite(this)" /></td>
<?php elseif ($student->status == 'ditawarkan') : ?>
			<td align="center"><input type="checkbox" name="invites[]" value="<?php echo $student->username.'@'.$student->name; ?>" onchange="saveinvite(this)" checked="checked" /></td>
<?php endif; ?>
		</tr>
<?php 
		endforeach;
	endif;
?>
	</tbody>
	</table>
<?php echo form_close(); ?>
	<div class="clear"><?php echo nbs(); ?></div>
</div>
